package heman.server.entities;

import java.util.ArrayList;



public class PlayerManager {
	static PlayerManager pm=null;
	private PlayerManager(){};
	public static PlayerManager getInstance(){
		if (pm==null)
			synchronized (PlayerManager.class) {
				if (pm==null)
					return pm=new PlayerManager();
				else return pm;
			}
			
		return pm;
	
	}
	ArrayList<Player> playerList=new ArrayList<Player>();
	public void addPlayer(Player p){
		synchronized (playerList){
			playerList.add(p);
		}
		
	}
	public boolean removePlayer(Player p){
		boolean b=false;
		synchronized (playerList){
			b=playerList.remove(p);
		}
		return b; 
	}
	public Player getPlayer(String id){
		Player player=null;
		synchronized (playerList) {
			for (Player p:playerList)
				if (p.getId().equals(id)){
					player=p;
					break;
				}
					 
		}
		return player;
	}
	/**
	 * 
	 * @return all players in XML representation
	 */
	public String getAllPlayersXML(){
		StringBuilder sb=new StringBuilder();
		
		synchronized (playerList){
			for (Player p:playerList)
				sb.append(p.toXMLPlayer());
			
		}
		return sb.toString();
	}
}
